package src.leetcode.backtrack.medium_0491;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class findSubsequences {
    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.findSubsequences(new int[]{4, 6, 7, 7}));
    }
}

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path= new ArrayList<>();
    int[] nums;
    public List<List<Integer>> findSubsequences(int[] nums) {
        this.nums = nums;
        dfs(0);
        return res;
    }

    private void dfs(int startIndex) {
        if(path.size()>= 2) res.add(new ArrayList<>(path));
        HashSet<Integer> hs = new HashSet<>();
        for(int i = startIndex; i < nums.length; i++){
            if(!path.isEmpty() && path.get(path.size() -1 ) > nums[i] || hs.contains(nums[i]))
                continue;
            hs.add(nums[i]);
            path.add(nums[i]);
            dfs(i + 1);
            path.remove(path.size() - 1);
        }
    }
}

